<?php
//引入初始化文件
include '../init.php';
//引入数据库连接文件
include DIR_CORE .'mysql.php';

$title = "考试结束 - 在线考试";

$name = trim($_GET['name']);
//提交时间
$time = time();

//测试时间
$ctime = $_POST['time'];

//计算所用时间(秒)
$ytime = $time-$ctime;
//转分钟秒钟
$ftime = floor($ytime/60);
$mtime = $ytime%60;
$strTime = $ftime.'分'.$mtime.'秒';
//var_dump($_POST);
$all = $_POST;
//echo '<br/>';

$trueAnswer = array();

/***获取题目id取答案****/
function getId($link, $p, $table)
{
    $sql = "select * from $table where id = $p ";
    $res = mysqli_query($link, $sql);
    $row = mysqli_fetch_assoc($res);
    $answer = str_replace(',', '', $row['answer']);
    //echo $answer.'、';
    if ($answer == 'true') {
        $answer='yes';
    } elseif ($answer == 'false') {
        $answer='no';
    }
    /**在函数中调用全局变量使用global***/
    global $trueAnswer;
    $trueAnswer[] = $answer;
}

/**
在php中使用foreach循环遍历时报Invalid argument supplied for foreach()错误，是因为循环的数据不是一个有效的数组。

因此我们只要在foreach之前判断一下数据源***/
foreach ($all as $k => $v) {
    if (is_array($v)) {
        foreach ($v as $k1 => $v1) {
            getId($link, $k1, $k);
        }
    }
}

$j=array();//用来存放考生答案judge
$m=array();//用来存放考生答案multiple
$f=array();//用来存放考生答案fill
$s=array();//用来存放考生答案single
/****循环传过来的表单数据，取出考生答案放入对应数组****/
foreach ($all as $k => $v) {
    if (is_array($k) || $k == 'judge') {
        $j[$k] = $v;
    }
    if (is_array($k) || $k == 'single') {
        $s[$k] = $v;
    }
    if (is_array($k) || $k == 'multiple') {
        $m[$k] = $v;
    }
    if (is_array($k) || $k == 'fill') {
        $f[$k] = $v;
    }
}

//var_dump($m);
//echo '<br/>';

/***提取judge,single,fill,multiple每个答案***/
$trueAnswerJudgeCount = array();
$trueAnswerSingleCount = array();
$trueAnswerFillCount = array();
$trueAnswerMultipleCount = array();

foreach ($trueAnswer as $k=>$v) {
    if ($k <= ($_POST['judgeCount']-1)) {
        $trueAnswerJudgeCount[] = $v;
    } elseif ($k <= ($_POST['singleCount'] + $_POST['judgeCount']-1)) {
        $trueAnswerSingleCount[]=$v;
    } elseif ($k <= ($_POST['multipleCount'] + $_POST['singleCount'] + $_POST['judgeCount'] -1)) {
        $trueAnswerMultipleCount[]=$v;
    } else {
        $trueAnswerFillCount[]=$v;
    }
}
/*
var_dump($trueAnswerJudgeCount);
echo '<br/>';
var_dump($trueAnswerSingleCount);
echo '<br/>';
var_dump($trueAnswerMultipleCount);
echo '<br/>';
var_dump($trueAnswerFillCount);
*/
include DIR_VIEW .'total.html';
